﻿package org.xhs.report.doc.model
{
    import __AS3__.vec.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.service.*;
    import baidu.wenku.common.ui.panel.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.utils.*;
    import flash.events.*;
    import flash.utils.*;

    public class DocLocatModel extends EventDispatcher implements ILocatModel
    {
        protected var _pageth:int = 0;
        protected var _pagesYCoordinate:Vector.<Number>;
        protected var _inLoading:Boolean;
        protected var _inSwitching:Boolean = false;
        protected var _timeStartLoading:Number;
        protected var _timeStayInPage:Number;
        protected var _timeBrowse:Number = 0.5;
        protected var _browseTimer:Timer;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var screenModel:ScreenModel;
        protected var storeModel:StoreModel;
        protected var storePage:StorePage;
        protected var docPageFlip:DocPageFlip;
        protected var docContentModel:DocContentModel;
        protected var _pagesArray:Vector.<BlankPage>;
        protected var _oriPageHeight:Number = 665;
        protected var _viewportHeight:Number = 665;

        public function DocLocatModel(param1:IEventDispatcher = null)
        {
            super(param1);
            this.initModel();
            this.initProvider();
            return;
        }// end function

        protected function initModel() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.storeModel = InstanceManager.createSingletonInstance(StoreModel);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            this._oriPageHeight = this.screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT;
            this._viewportHeight = this.screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT;
            this._browseTimer = new Timer(this._timeBrowse * 1000, 1);
            this._browseTimer.addEventListener(TimerEvent.TIMER, this.browseTimer);
            return;
        }// end function

        protected function initProvider() : void
        {
            this.docContentModel = InstanceManager.createSingletonInstance(DocContentModel);
            this.docContentModel.addEventListener(PageServiceEvent.DOCINFO_LOADED, this.docInfoLoaded);
            this.docContentModel.addEventListener(PageServiceEvent.START_LOADING, this.onStartLoading);
            this.docContentModel.addEventListener(PageServiceEvent.PAGE_LOADED, this.pageLoaded);
            this.docContentModel.addEventListener(PageServiceEvent.PAGE_FAILED, this.pageFailed);
            this.docContentModel.addEventListener(PageServiceEvent.FILE_DELETED, this.fileDeleted);
            this.docContentModel.addEventListener(PageServiceEvent.FILE_NO_DATA, this.fileNoData);
            this.docContentModel.addEventListener(PageServiceEvent.FIRST_PARSE_BYTE, this.firstParseByte);
            this.docContentModel.addEventListener(PageServiceEvent.PARSE_FAILED, this.onParseFailed);
            this.docContentModel.addEventListener(PageServiceEvent.BLANKPAGE_ADDED, this.onBlankpageAdded);
            this.docContentModel.addEventListener(PageServiceEvent.PAGE_REARRANGE, this.onPageRearrange);
            return;
        }// end function

        protected function docInfoLoaded(event:PageServiceEvent) : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.ADD_2_SCROLLPANE));
            return;
        }// end function

        protected function onStartLoading(event:PageServiceEvent) : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.START_LOADING));
            return;
        }// end function

        protected function pageLoaded(event:PageServiceEvent) : void
        {
            return;
        }// end function

        protected function pageFailed(event:PageServiceEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_LOADDOCFAIL + "&docid=" + this.loaderInfoVo.docId + "&reason=noResponse");
            this._inLoading = false;
            dispatchEvent(new LocatEvent(LocatEvent.PROCESS_NO_DOC));
            return;
        }// end function

        protected function fileDeleted(event:PageServiceEvent) : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.PROCESS_DEL_DOC));
            return;
        }// end function

        protected function fileNoData(event:PageServiceEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_LOADDOCFAIL + "&docid=" + this.loaderInfoVo.docId + "&reason=noData");
            dispatchEvent(new LocatEvent(LocatEvent.PROCESS_NO_DOC));
            this._inLoading = false;
            return;
        }// end function

        protected function firstParseByte(event:PageServiceEvent) : void
        {
            var _loc_2:* = new Date().time - this._timeStartLoading;
            if (!Config.isAPIViewer)
            {
                WenkuLog.addLog(WenkuLog.LOGURL_LOADTIME + _loc_2);
                this._browseTimer.reset();
                this._browseTimer.start();
            }
            return;
        }// end function

        protected function onParseFailed(event:PageServiceEvent) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_LOADDOCFAIL + "&docid=" + this.loaderInfoVo.docId + "&reason=parseError");
            return;
        }// end function

        protected function onBlankpageAdded(event:PageServiceEvent) : void
        {
            this._pagesArray = this.docContentModel.pagesArray;
            this._pageth = 0;
            dispatchEvent(new LocatEvent(LocatEvent.STOP_LOADING));
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_LOADED));
            this._timeStayInPage = new Date().time;
            if (this.loaderInfoVo.bookmarkPage && this.loaderInfoVo.bookmarkPage <= this.docContentModel.pagesAllAvailable)
            {
                this.pageth = this.loaderInfoVo.bookmarkPage - 1;
            }
            dispatchEvent(new LocatEvent(LocatEvent.BLANKPAGE_ADDED));
            return;
        }// end function

        protected function onPageRearrange(event:PageServiceEvent) : void
        {
            if (!this._pagesArray)
            {
                return;
            }
            dispatchEvent(new LocatEvent(LocatEvent.PAGE_REARRANGED));
            return;
        }// end function

        protected function loadData() : void
        {
            dispatchEvent(new LocatEvent(LocatEvent.SHOW_LOADING));
            this.docContentModel.loadData();
            return;
        }// end function

        protected function screenResize(event:Event) : void
        {
            var cursorID:int;
            var e:* = event;
            if (!(this.screenModel.stage.stageHeight > 0 && this.screenModel.stage.stageWidth > 0))
            {
                return;
            }
            if (this.docContentModel.pagesAllAvailable <= 0)
            {
                return;
            }
            if (!this.docContentModel.pagethLoading)
            {
                return;
            }
            if (this.screenModel.isFullscreen)
            {
                this.inSwitching = true;
                cursorID = setInterval(function () : void
            {
                clearInterval(cursorID);
                inSwitching = false;
                return;
            }// end function
            , 4000);
            }
            this._viewportHeight = this.screenModel.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT;
            return;
        }// end function

        protected function browseTimer(event:TimerEvent = null) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_BROWSER + "&docid=" + this.loaderInfoVo.docId + "&page=" + (this._pageth + 1));
            return;
        }// end function

        public function init() : void
        {
            this.docContentModel.docFpth = 0;
            this.loadData();
            return;
        }// end function

        public function docScroll(param1:int) : void
        {
            var _loc_2:Number = NaN;
            if (this._pageth != param1)
            {
                if (!this._timeStayInPage)
                {
                    this._timeStayInPage = new Date().time;
                }
                else
                {
                    _loc_2 = (new Date().time - this._timeStayInPage) / 1000;
                    this._timeStayInPage = new Date().time;
                    if (_loc_2 >= Config.TIME_IN_PAGE)
                    {
                        WenkuLog.addLog(WenkuLog.LOGURL_STAYINPAGE + "&docid=" + this.loaderInfoVo.docId + "&page=" + (this._pageth + 1) + "&time=" + _loc_2);
                    }
                }
                this._pageth = param1;
                this.docContentModel.pageth = this._pageth;
                dispatchEvent(new LocatEvent(LocatEvent.PAGETH_CHANGED_PAGEGROUP));
                if (!Config.isAPIViewer)
                {
                    this._browseTimer.reset();
                    this._browseTimer.start();
                }
            }
            return;
        }// end function

        public function isPagethLoaded(param1:int) : Boolean
        {
            return this.docContentModel.isPagethLoaded(param1);
        }// end function

        public function set pageth(param1:int) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:int = 0;
            var _loc_4:Boolean = false;
            var _loc_5:Number = NaN;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            param1 = Math.max(0, param1);
            param1 = Math.min((this.docContentModel.pagesAllAvailable - 1), param1);
            if (this.docPageFlip)
            {
                this.docPageFlip.unload();
            }
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_DOUBLE)
            {
                _loc_3 = this._pageth;
                this._pageth = param1;
                this.docContentModel.pageth = this._pageth;
                dispatchEvent(new LocatEvent(LocatEvent.PAGETH_CHANGED_PAGEGROUP));
                dispatchEvent(new LocatEvent(LocatEvent.PAGETH_CHANGED_SCROLL));
                if (Math.floor(param1 / 2) != Math.floor(_loc_3 / 2))
                {
                    if (!this._timeStayInPage)
                    {
                        this._timeStayInPage = new Date().time;
                    }
                    else
                    {
                        _loc_2 = (new Date().time - this._timeStayInPage) / 1000;
                        this._timeStayInPage = new Date().time;
                        if (_loc_2 >= Config.TIME_IN_PAGE)
                        {
                            WenkuLog.addLog(WenkuLog.LOGURL_STAYINPAGE + "&docid=" + this.loaderInfoVo.docId + "&page=" + (this._pageth + 1) + "&time=" + _loc_2);
                        }
                    }
                    if (!this.docPageFlip)
                    {
                        this.docPageFlip = new DocPageFlip();
                        this.docPageFlip.pagesArray = this._pagesArray;
                        if (this.storePage)
                        {
                            this.docPageFlip.storePage = this.storePage;
                        }
                    }
                    _loc_4 = param1 > _loc_3;
                    this.docPageFlip.flip(param1, _loc_3, _loc_4);
                }
                return;
            }
            if (param1 != this._pageth)
            {
                if (!this._timeStayInPage)
                {
                    this._timeStayInPage = new Date().time;
                }
                else
                {
                    _loc_2 = (new Date().time - this._timeStayInPage) / 1000;
                    this._timeStayInPage = new Date().time;
                    if (_loc_2 >= Config.TIME_IN_PAGE)
                    {
                        WenkuLog.addLog(WenkuLog.LOGURL_STAYINPAGE + "&docid=" + this.loaderInfoVo.docId + "&page=" + (this._pageth + 1) + "&time=" + _loc_2);
                    }
                }
                _loc_5 = param1 - this._pageth;
                this._pageth = param1;
                this.docContentModel.pageth = this._pageth;
                if (this.loaderInfoVo.isPPT)
                {
                    _loc_6 = 0;
                    _loc_7 = this._pagesArray.length;
                    while (_loc_6 < _loc_7)
                    {
                        
                        this._pagesArray[_loc_6].visible = false;
                        _loc_6++;
                    }
                    this._pagesArray[this._pageth].visible = true;
                    this._pagesArray[this._pageth].showLoading();
                    if (this.storePage && this._pagesArray[this._pageth] == this.storePage)
                    {
                        this._pagesArray[this._pageth].hideLoading();
                    }
                }
                dispatchEvent(new LocatEvent(LocatEvent.PAGETH_CHANGED_PAGEGROUP));
                dispatchEvent(new LocatEvent(LocatEvent.PAGETH_CHANGED_SCROLL));
            }
            return;
        }// end function

        public function get pageth() : int
        {
            return this._pageth;
        }// end function

        public function get pagesLoaded() : int
        {
            return this.docContentModel.pagesLoaded;
        }// end function

        public function get pagesAllAvailable() : int
        {
            return this.docContentModel.pagesAllAvailable;
        }// end function

        public function get pagesAllTotally() : int
        {
            return this.docContentModel.pagesAllTotally;
        }// end function

        public function get pagesLoaderArray() : Vector.<Loader>
        {
            return this.docContentModel.pagesLoaderArray;
        }// end function

        public function set inSwitching(param1:Boolean) : void
        {
            this._inSwitching = param1;
            dispatchEvent(new LocatEvent(LocatEvent.FULLSCREEN_SWITCHING));
            return;
        }// end function

        public function get inSwitching() : Boolean
        {
            return this._inSwitching;
        }// end function

        public function get pagethLoading() : int
        {
            return this.docContentModel.pagethLoading;
        }// end function

        public function get pagesYCoordinate() : Vector.<Number>
        {
            return this._pagesYCoordinate;
        }// end function

        public function set pagesYCoordinate(param1:Vector.<Number>) : void
        {
            this._pagesYCoordinate = param1;
            return;
        }// end function

        public function get oriPageHeight() : Number
        {
            return this._oriPageHeight;
        }// end function

        public function get viewportHeight() : Number
        {
            return this._viewportHeight;
        }// end function

        public function get pagesArray() : Vector.<BlankPage>
        {
            return this._pagesArray;
        }// end function

    }
}
